package api

import (
	"lianzi_gos/internal/mods/mpweixin/biz"
	"lianzi_gos/internal/mods/mpweixin/schema"
	"lianzi_gos/pkg/util"

	"github.com/gin-gonic/gin"
)

// 产品属性表
type GoodsAttr struct {
	GoodsAttrBIZ *biz.GoodsAttr
}

// @Tags GoodsAttrAPI
// @Security ApiKeyAuth
// @Summary Query goods attr list
// @Param year query string false "年限"
// @Param quality query string false "成色"
// @Param thickness query string false "厚度"
// @Success 200 {object} util.ResponseResult{data=[]schema.GoodsAttr}
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/goods-attrs [get]
func (a *GoodsAttr) Query(c *gin.Context) {
	ctx := c.Request.Context()
	var params schema.GoodsAttrQueryParam
	if err := util.ParseQuery(c, &params); err != nil {
		util.ResError(c, err)
		return
	}

	result, err := a.GoodsAttrBIZ.Query(ctx, params)
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResPage(c, result.Data, result.PageResult)
}

// @Tags GoodsAttrAPI
// @Security ApiKeyAuth
// @Summary Get goods attr record by ID
// @Param id path string true "unique id"
// @Success 200 {object} util.ResponseResult{data=schema.GoodsAttr}
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/goods-attrs/{id} [get]
func (a *GoodsAttr) Get(c *gin.Context) {
	ctx := c.Request.Context()
	item, err := a.GoodsAttrBIZ.Get(ctx, c.Param("id"))
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResSuccess(c, item)
}

// @Tags GoodsAttrAPI
// @Security ApiKeyAuth
// @Summary Create goods attr record
// @Param body body schema.GoodsAttrForm true "Request body"
// @Success 200 {object} util.ResponseResult{data=schema.GoodsAttr}
// @Failure 400 {object} util.ResponseResult
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/goods-attrs [post]
func (a *GoodsAttr) Create(c *gin.Context) {
	ctx := c.Request.Context()
	item := new(schema.GoodsAttrForm)
	if err := util.ParseJSON(c, item); err != nil {
		util.ResError(c, err)
		return
	} else if err := item.Validate(); err != nil {
		util.ResError(c, err)
		return
	}

	result, err := a.GoodsAttrBIZ.Create(ctx, item)
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResSuccess(c, result)
}

// @Tags GoodsAttrAPI
// @Security ApiKeyAuth
// @Summary Update goods attr record by ID
// @Param id path string true "unique id"
// @Param body body schema.GoodsAttrForm true "Request body"
// @Success 200 {object} util.ResponseResult
// @Failure 400 {object} util.ResponseResult
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/goods-attrs/{id} [put]
func (a *GoodsAttr) Update(c *gin.Context) {
	ctx := c.Request.Context()
	item := new(schema.GoodsAttrForm)
	if err := util.ParseJSON(c, item); err != nil {
		util.ResError(c, err)
		return
	} else if err := item.Validate(); err != nil {
		util.ResError(c, err)
		return
	}

	err := a.GoodsAttrBIZ.Update(ctx, c.Param("id"), item)
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResOK(c)
}

func (a *GoodsAttr) UpdateStatus(c *gin.Context) {
	ctx := c.Request.Context()
	item := new(schema.GoodsAttrForm)
	if err := util.ParseJSON(c, item); err != nil {
		util.ResError(c, err)
		return
	} else if err := item.Validate(); err != nil {
		util.ResError(c, err)
		return
	}

	err := a.GoodsAttrBIZ.Update(ctx, c.Param("id"), item)
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResOK(c)
}
func (a *GoodsAttr) UpdateAttrStatus(c *gin.Context) {
	ctx := c.Request.Context()
	item := new(schema.GoodsAttrStatus)
	if err := util.ParseJSON(c, item); err != nil {
		util.ResError(c, err)
		return
	}

	err := a.GoodsAttrBIZ.UpdateAttrStatus(ctx, c.Param("id"), item)
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResOK(c)
}
func (a *GoodsAttr) UpdateAttrPrice(c *gin.Context) {
	ctx := c.Request.Context()
	item := new(schema.GoodsAttrPrice)
	if err := util.ParseJSON(c, item); err != nil {
		util.ResError(c, err)
		return
	}

	err := a.GoodsAttrBIZ.UpdateAttrPrice(ctx, c.Param("id"), item)
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResOK(c)
}

// @Tags GoodsAttrAPI
// @Security ApiKeyAuth
// @Summary Delete goods attr record by ID
// @Param id path string true "unique id"
// @Success 200 {object} util.ResponseResult
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/goods-attrs/{id} [delete]
func (a *GoodsAttr) Delete(c *gin.Context) {
	ctx := c.Request.Context()
	err := a.GoodsAttrBIZ.Delete(ctx, c.Param("id"))
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResOK(c)
}
